<template>
  <div>
    <!-- 卡片视图 -->
    <el-card class="box-card">
      <div class="madiv">
        <div>
          <el-form :inline="true">
            <el-form-item
              ><el-input placeholder="请输入教材名" v-model="keyword"></el-input
            ></el-form-item>
            <el-form-item>
              <el-button
                type="primary"
                icon="el-icon-search"
                @click="handleQuery"
                >搜 索</el-button
              >
            </el-form-item>
          </el-form>
        </div>
        <div>
          <el-form :inline="true">
            <el-form-item>
              <el-button
                class="el-icon-plus"
                @click="handleAdd()"
                type="success"
                >新 增</el-button
              >
            </el-form-item>
          </el-form>
        </div>
      </div>
    </el-card>
    <!-- 教材信息列表 -->
    <el-card>
      <el-table :data="tbData" border style="width: 100%" v-loading="loading">
        <el-table-column type="selection" width="45"></el-table-column>
        <el-table-column label="教材名" align="center" property="materialName">
        </el-table-column>
        <el-table-column
          label="价格"
          align="center"
          property="price"
        ></el-table-column>
        <el-table-column
          label="出版社"
          align="center"
          property="press"
        ></el-table-column>
        <el-table-column
          label="联系电话"
          align="center"
          property="phone"
        ></el-table-column>
        <!-- 弹出框 -->
        <el-table-column label="操作" align="center" fixed="right">
          <template slot-scope="scope" width="150px">
            <el-button
              size="mini"
              type="warning"
              @click="handleEdit(scope.$index, scope.row)"
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 模态框 -->
      <div>
        <el-dialog
          title="新增教材"
          :visible.sync="dialogFormVisible"
        >
          <el-form
            :model="form"
            :rules="rules"
            ref="formDemo"
            label-width="100px"
            class="mafrom"
          >
            <el-form-item label="教材名:" prop="materialName">
              <el-input
                v-model="form.materialName"
                controls-position="right"
              ></el-input>
            </el-form-item>
            <el-form-item label="价格:" prop="price">
              <el-input v-model="form.price" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="出版社:" prop="press">
              <el-input v-model="form.press" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="电话:" prop="contactNumber">
              <el-input
                v-model="form.contactNumber"
                controls-position="right"
              ></el-input>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="handleCancel">取 消</el-button>
            <el-button type="primary" @click="handleSave">确 定</el-button>
          </div>
        </el-dialog>
      </div>
      <div class="block">
        <el-pagination
          :current-page="pager.pageIndex"
          :page-size="pager.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="pager.rowsTotal"
          background
        >
        </el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
import { GetMaterialList } from "@/api/materialapi";
export default {
  created() {
    GetMaterialList().then((res) => {
      console.log(res.data);
      this.tbData = res.data;
    });
  },
  data() {
    // 1. 验证手机号的验证规则
    var checkMobile = (rule, value, callback) => {
      // 前两位以13、15、18、17开头 后八位0-9
      const reg =
        /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
      if (reg.test(value)) {
        // 验证通过
        return callback();
      } else {
        // 验证不通过
        callback(new Error("手机号不正确"));
      }
    };
    return {
      pager: {
        pageIndex: 1, //当前页面
        pageSize: 6, //每页展示列表数
        rowsTotal: 20, // 列表总数
      },
      rules: {
        materialName: [
          { required: true, trigger: "blur", message: "请输入教材名" },
        ],
        price: [{ required: true, trigger: "blur", message: "请输入价格" }],
        press: [{ required: true, trigger: "blur", message: "请输入出版社" }],
        contactNumber: [
          { required: true, trigger: "blur", message: "请输入联系电话" },
          { validator: checkMobile, trigger: "blur" },
        ],
      },
      keyword: "",
      loading: false,
      tbData: [],
      dialogFormVisible: false,
      form: {
        id: "",
        materialName: "",
        price: "",
        press: "",
        contactNumber: "",
      },
    };
  },

  methods: {
    // 搜索
    handleQuery() {},
    //  刷新
    handleRefresh() {},
    //  新增
    handleAdd() {
      this.dialogFormVisible = true;
    },
    // 编辑
    handleEdit(index) {
      this.dialogFormVisible = true;
      // 编辑时获取当前表数组中的下标
      this.currentEditIndex = index;
    },
    // 删除教材
    handleDelete() {},
    // 取消
    handleCancel() {
      this.dialogFormVisible = false;
    },
    // 保存
    handleSave() {},
  },
};
</script>
<style>
.madiv {
  display: flex;
  float: left;
}
.block {
  margin-top: 30px;
  margin-bottom: 10px;
}
.mafrom > .el-form-item > .el-form-item__label {
  font-weight: bolder !important;
  font-size: 18px;
}
</style>
